﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;

using Warehouse.Service.Data;
using Warehouse.Silverlight.Core.Infrastructure;
using Warehouse.Silverlight.Core.Infrastructure.ViewModels;
using Warehouse.Silverlight.Modules.Main.Models;
using Warehouse.Silverlight.Modules.Main.Services;

namespace Warehouse.Silverlight.Modules.Main.ViewModels
{
    [Export]
    public class TestListViewModel : ViewModel
    {
        private readonly IProductService _productService;
        private readonly IEventAggregator _eventAggregator;
        private ObservableCollection<Product> _items;
        private Product _selectedItem;

        [ImportingConstructor]
        public TestListViewModel(IProductService productService, IEventAggregator eventAggregator)
        {
            _productService = productService;
            _eventAggregator = eventAggregator;

            Initialize();
        }

        public ObservableCollection<Product> Items { get { return _items; } }

        public Product SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    RaisePropertyChanged(() => SelectedItem);

                    if (_selectedItem != null)
                    {
                        var e = _eventAggregator.GetEvent<SelectedProductChangedEvent>();
                        e.Publish(_selectedItem.ProductId);
                    }
                }
            }
        }

        // Override
        protected override void Initialize()
        {
            _items = _productService.GetAllProducts();
        }

        #region private Helpers


        #endregion
    }
}
